package org.example.bombermanclient.controller;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;

import org.example.bombermanclient.listener.MessageListener;
import org.example.bombermanclient.model.Message;
import org.example.bombermanclient.network.NettyClient;

public class MainController {

    @FXML private Pane gamePane;

    private NettyClient nettyClient = new NettyClient();
    private String playerId = "111111";

    private ImageView playerView;
    private double playerX = 100; // 初始位置
    private double playerY = 100;
    private final double moveStep = 10; // 每次移动步长

    @FXML
    public void initialize() {
        // 创建 ImageView
        Image playerImage = new Image("file:E:/lbl.jpg");
        playerView = new ImageView(playerImage);
        playerView.setFitWidth(50);
        playerView.setFitHeight(50);
        playerView.setLayoutX(playerX);
        playerView.setLayoutY(playerY);
        gamePane.getChildren().add(playerView);

        // 设置消息监听器
        nettyClient.setListener(new MessageListener() {
            @Override
            public void onMessageReceived(Message message) {
                Platform.runLater(() -> {
                    if ("MOVE".equals(message.getType()) && playerId.equals(message.getPlayerId())) {
                        if (message.getX() != null) {
                            playerX = message.getX();
                        }
                        if (message.getY() != null) {
                            playerY = message.getY();
                        }
                        playerView.setLayoutX(playerX);
                        playerView.setLayoutY(playerY);
                    }
                });
            }

            @Override
            public void onConnected() {}

            @Override
            public void onDisconnected() {}
        });

        // 异步连接服务器
        new Thread(() -> {
            try {
                nettyClient.connect("localhost", 9000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    // 绑定键盘事件
    public void bindKeyEvents(Scene scene) {
        scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
            double newX = playerX;
            double newY = playerY;
            String direction = null;

            if (event.getCode() == KeyCode.UP) {
                newY -= moveStep;
                direction = "UP";
            } else if (event.getCode() == KeyCode.DOWN) {
                newY += moveStep;
                direction = "DOWN";
            } else if (event.getCode() == KeyCode.LEFT) {
                newX -= moveStep;
                direction = "LEFT";
            } else if (event.getCode() == KeyCode.RIGHT) {
                newX += moveStep;
                direction = "RIGHT";
            }

            if (direction != null) {
                // 限制边界
                newX = Math.max(0, Math.min(newX, gamePane.getWidth() - playerView.getFitWidth()));
                newY = Math.max(0, Math.min(newY, gamePane.getHeight() - playerView.getFitHeight()));

                playerX = newX;
                playerY = newY;
                playerView.setLayoutX(playerX);
                playerView.setLayoutY(playerY);

                // 发送 MOVE 消息
                Message moveMsg = new Message();
                moveMsg.setType("MOVE");
                moveMsg.setPlayerId(playerId);
                moveMsg.setX((int) playerX);
                moveMsg.setY((int) playerY);
                moveMsg.setDirection(direction);
                nettyClient.sendMessage(moveMsg);

                event.consume();
            }
        });
    }

    public void onClose() {
        nettyClient.disconnect();
    }
}
